﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Entities;
using BaHuan.Common;

namespace BaHuan
{
    public partial class FrmDoiTraHang : Form
    {
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();
        private KhachHangManagement khachhangMgt = new KhachHangManagement();
        private PhieuXuatHangManagement phieuxuathangMgt = new PhieuXuatHangManagement();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmDoiTraHang()
        {
            InitializeComponent();
        }

        private void FrmDoiTraHang_Load(object sender, EventArgs e)
        {
            BindingToCbo();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            if (dgvListPX.CurrentRow != null)
            {
                PhieuXuatHang phieuxuathang = (PhieuXuatHang)dgvListPX.CurrentRow.DataBoundItem;
                string tienchu = CNum2Char.So2Chu((long)Convert.ToDouble(phieuxuathang.TongCong));
                FrmXemReport frmView = new FrmXemReport(phieuxuathang, tienchu);
                this.Hide();
                frmView.ShowDialog();
                this.Show();
            }
        }

        private void btnHangTra_Click(object sender, EventArgs e)
        {
            if (dgvListPX.CurrentRow != null)
            {
                PhieuXuatHang phieuxuathang = (PhieuXuatHang)dgvListPX.CurrentRow.DataBoundItem;
                FrmPhieuHangTra frmPhieuHangTra = new FrmPhieuHangTra(phieuxuathang);
                frmPhieuHangTra.NhanVien = this.nhanvien;
                this.Hide();
                frmPhieuHangTra.ShowDialog();
                this.Show();
            }
        }

        private void btnDoiHang_Click(object sender, EventArgs e)
        {
            if (dgvListPX.CurrentRow != null)
            {
                PhieuXuatHang phieuxuathang = (PhieuXuatHang)dgvListPX.CurrentRow.DataBoundItem;
                FrmPhieuDoiHang frmPhieuDoiHang = new FrmPhieuDoiHang(phieuxuathang, NhanVien);
                this.Hide();
                frmPhieuDoiHang.ShowDialog();
                this.Show();
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            string khachhang = "";
            int chinhanh = -1;

            if(cboKhachHang.SelectedItem != null)
               khachhang = ((KhachHang)cboKhachHang.SelectedItem).Ten;

            if(cboNoiXuat.SelectedItem != null)
                chinhanh = ((ChiNhanhCongTy)cboNoiXuat.SelectedItem).Id;

            List<PhieuXuatHang> lstPhieuXuatHang = (from p in GetAllPhieuXuatHang()
                                                    where (p.KhachHang == khachhang
                                                        && p.IdChiNhanhCongTy == chinhanh)
                                                        && (p.NgayThang.Date >= dtpTuNgay.Value.Date && p.NgayThang.Date <= dtpDenNgay.Value.Date)
                                                    select p).ToList();

            dgvListPX.AutoGenerateColumns = false;
            dgvListPX.DataSource = lstPhieuXuatHang;
        }

        private void dgvListPX_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvListPX.CurrentRow != null)
            {
                PhieuXuatHang phieuxuathang = (PhieuXuatHang)dgvListPX.CurrentRow.DataBoundItem;
                FrmKiemTraPhieuXuatHang frmKiemTraPhieuXuatHang = new FrmKiemTraPhieuXuatHang();
                frmKiemTraPhieuXuatHang.PhieuXuatHang = phieuxuathang;
                this.Hide();
                frmKiemTraPhieuXuatHang.ShowDialog();
                this.Show();
            }
        }

        public void BindingToCbo()
        {
            cboKhachHang.DataSource = GetAllKhachHang();
            cboKhachHang.DisplayMember = "Ten";
            cboKhachHang.ValueMember = "Id";

            cboNoiXuat.DataSource = GetAllChiNhanhCongTy();
            cboNoiXuat.DisplayMember = "TenChiNhanh";
            cboNoiXuat.ValueMember = "Id";
        }

        private void cboNoiXuat_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy b in cboNoiXuat.Items)
            {
                if (b.TenChiNhanh.ToLower().Contains(cboNoiXuat.Text) &&cboNoiXuat.Text!="")
                {
                    cboNoiXuat.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboNoiXuat.SelectedValue == null)
            {
                cboNoiXuat.Text = "";
            }
        }

        private void cboKhachHang_Leave(object sender, EventArgs e)
        {
            foreach (KhachHang b in cboKhachHang.Items)
            {
                if (b.Ten.ToLower().Contains(cboKhachHang.Text) && cboKhachHang.Text!="")
                {
                    cboKhachHang.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboKhachHang.SelectedValue == null)
            {
                cboKhachHang.Text = "";
            }
        }        

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;

            return listChiNhanhCongTy;
        }

        public List<KhachHang> GetAllKhachHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = khachhangMgt.GetAllKhachHang();
            List<KhachHang> listKhachHang = (List<KhachHang>)resultObj.Result;

            return listKhachHang;
        }

        public List<PhieuXuatHang> GetAllPhieuXuatHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieuxuathangMgt.GetAllPhieuXuatHang();
            List<PhieuXuatHang> listPhieuXuatHang = (List<PhieuXuatHang>)resultObj.Result;

            return listPhieuXuatHang;
        }
    }
}
